const MAX_LIMIT=100 
var app = getApp()
const db = wx.cloud.database();
Page({ 
 
  /** 
   * 页面的初始数据 
   */ 
  data: { 
    commentList: [],
    tupianList: [],
    shipinList: [],
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
  }, 
  
//  tab切换逻辑
swichNav: function( e ) {

    var that = this;

    if( this.data.currentTab === e.target.dataset.current ) {
        return false;
    } else {
        that.setData( {
            currentTab: e.target.dataset.current
        })
    }
},

bindChange: function( e ) {

    var that = this;
    that.setData( { currentTab: e.detail.current });

},

  /** 
   * 生命周期函数--监听页面加载 
   */ 
  onLoad: function (options) { 
  
this._getListByCloudfu() 
this._getListByCloudfu1() 
this._getListByCloudfu2() 
    /**
     * 获取当前设备的宽高
     */  var that = this;
    wx.getSystemInfo( {

        success: function( res ) {
            that.setData( {
                winWidth: res.windowWidth,
                winHeight: res.windowHeight
            });
        }

    });




  }, 

// 漫贴
_getListByCloudfu(){ 
  wx.showLoading({ 
    title: '加载中', 
  }) 
  wx.cloud.callFunction({ 
    name:'mantie', 
    data:{ 
      $url:'getListByOpenid', 
      start:this.data.commentList.length, 
      count:MAX_LIMIT 
    } 
 
  }).then((res)=>{ 
    console.log(res) 
    this.setData({ 
      commentList:res.result
    }) 
    var sum_heigth = 0
	
        for (let i = 0; i < this.data.commentList.length; i++) {
            sum_heigth ++
        }
        this.setData({
            mantie_height: sum_heigth
        })
    wx.hideLoading() 
  }) 
}, 
goComment(event) {
 
  wx.navigateTo({
    url: '/pages/index/mantie/mantie-nr/mantie-nr?tieziId=' + event.target.dataset.tieziid,
  })
},

// 视频
_getListByCloudfu2(){ 
  wx.showLoading({ 
    title: '加载中', 
  }) 
  wx.cloud.callFunction({ 
    name:'shipin', 
    data:{ 
      $url:'getListByOpenid', 
      start:this.data.commentList.length, 
      count:MAX_LIMIT 
    } 
 
  }).then((res)=>{ 
    console.log(res) 
    this.setData({ 
      shipinList:res.result
    }) 
    wx.hideLoading() 
  }) 
}, 
goComment2(event) {
 
  wx.navigateTo({
    url: '/pages/index/shipin/shipin-nr/shipin-nr?shipinId=' + event.target.dataset.shipinid,
  })
},

// 图片

 
_getListByCloudfu1() {
  wx.showLoading({ 
    title: '加载中', 
  }) 
  wx.cloud.callFunction({ 
    name:'meitu', 
    data:{ 
      $url:'getListByOpenid', 
      start:this.data.commentList.length, 
      count:MAX_LIMIT 
    } 
 
  }).then((res)=>{ 
    console.log(res) 
    this.setData({ 
      tupianList:res.result
    }) 
    wx.hideLoading() 
  }) 
},
goComment1(event) {
 
  wx.navigateTo({
    url: '/pages/index/meitu/meitu-nr/meitu-nr?tupianId=' + event.target.dataset.tupianid,
  })
},
remove:function(e){
  let id = e.currentTarget.dataset.id
  console.log(e.currentTarget.dataset.index)
  const db = wx.cloud.database();
  db.collection("tupian").doc(id).remove({
    success:res=>{
      wx.showToast({
        title: '删除成功',
      })
      var tupianList=this.data.tupianList;
      tupianList.splice(e.target.dataset.index,1);
      this.setData({
        tupianList:tupianList
      })
    },fail:err=>{
      wx.showToast({
        title: '删除失败',
      })
    }
  })

},
remove1:function(e){
  let id = e.currentTarget.dataset.id
  console.log(id)
  const db = wx.cloud.database();
  db.collection("tiezi").doc(id).remove({
    success:res=>{
      wx.showToast({
        title: '删除成功',
      })
      var commentList=this.data.commentList;
      commentList.splice(e.target.dataset.index,1);
      this.setData({
        commentList:commentList
      })
    },fail:err=>{
      wx.showToast({
        title: '删除失败',
      })
    }
  })

},
remove2:function(e){
  let id = e.currentTarget.dataset.id
  console.log(id)
  const db = wx.cloud.database();
  db.collection("shipin").doc(id).remove({
    success:res=>{
      wx.showToast({
        title: '删除成功',
      })
      var shipinList=this.data.shipinList;
      shipinList.splice(e.target.dataset.index,1);
      this.setData({
        shipinList:shipinList
      })//删除成功重新加载
    },fail:err=>{
      wx.showToast({
        title: '删除失败',
      })
    }
  })

},
  /** 
   * 生命周期函数--监听页面初次渲染完成 
   */ 
  onReady: function () { 
 
  }, 
 
  /** 
   * 生命周期函数--监听页面显示 
   */ 
  onShow: function () { 
 
  }, 
 
  /** 
   * 生命周期函数--监听页面隐藏 
   */ 
  onHide: function () { 
 
  }, 
 
  /** 
   * 生命周期函数--监听页面卸载 
   */ 
  onUnload: function () { 
 
  }, 
 
  /** 
   * 页面相关事件处理函数--监听用户下拉动作 
   */ 
  onPullDownRefresh: function () { 
    
  }, 
 
  /** 
   * 页面上拉触底事件的处理函数 
   */ 
  onReachBottom: function () { 
    this._getListByCloudfu() 
  }, 
 
  /** 
   * 用户点击右上角分享 
   */ 
  onShareAppMessage: function () { 
 
  } 
})
